|
operator new[]function
<new>
void* operator new[] (std::size_t size) throw (std::bad_alloc); void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_constant) throw(); void* operator new[] (std::size_t size, void* ptr) throw(); Allocate storage space for array The first version allocates size bytes of storage space, aligned to represent an array object of that size (or less, if the implementation uses array overhead), and returns a non-null pointer to the first byte of this block. On failure, it throws a bad_alloc exception.The second version is the nothrow version. It does the same as the first version, except that on failure it turns a null pointer instead of throwing an exception. The third version is the placement version, that does not allocate memory - it simply returns ptr. Notice though that the constructor for the object will still be called. Global dynamic storage operator functions are special in the standard library:
If set_new_handler has been used to define a new_handler function, this new_handler function is called by the standard default definition of operator new[] if it cannot allocate the requested storage by its own. operator new[] can be called explicitly as a regular function, but in C++, new[] is an operator with a very specific behavior: An expression with new and an array type specifier is an operator new[] expression. This, first calls function operator new[] with the size of its array type specifier as first argument (plus any array overhead storage to keep track of the size, if any), and if this is successful, it then automatically initializes or constructs each of the objects in the array (if needed). The expression evaluates as a pointer to the appropriate type pointing to the first element of the array. Parameters
Return valueFor the first and second versions, a pointer to the first element in the newly allocated storage space.For the third version, ptr is returned. Example
Output:
See also
|